/**
 *@description:
 *@file: video_read.cpp
 *@author: snow-tyan
 *@email: zziywang@163.com
 *@date: 8 March 2023
 *@copyright: 2018-2022 AiRiA. All rights reserved.
 */

#include <iostream>
#include <opencv2/opencv.hpp>

#ifndef PROJECT_ROOT
#define PROJECT_ROOT "F:/VisualStudioProjects/my-open-gl"
#endif

const std::string kVideoRoot = PROJECT_ROOT + std::string("/resources/videos/");

int main() {
  std::string filename = kVideoRoot + "table_wb.mp4";
  cv::VideoCapture video_cap;
  if (!video_cap.open(filename)) {
    fprintf(stderr, "no such file: %s\n", filename.c_str());
    exit(-1);
  }
  auto fps = video_cap.get(cv::CAP_PROP_FPS);
  auto frame_all = video_cap.get(cv::CAP_PROP_FRAME_COUNT);
  printf("video fps: %f\n", fps);
  printf("video frame_all: %f\n", frame_all);
  printf("video duration: %.4fs\n", frame_all / fps);
  auto frame_h = video_cap.get(cv::CAP_PROP_FRAME_HEIGHT);
  auto frame_w = video_cap.get(cv::CAP_PROP_FRAME_WIDTH);
  printf("frame height: %f, width: %f\n", frame_h, frame_w);

  cv::Mat frame;
  cv::namedWindow("Video", cv::WINDOW_AUTOSIZE);
  while (video_cap.read(frame)) {
    cv::imshow("Video", frame);
    char c = cv::waitKey(1);
    if (c == 27) {
      cv::destroyWindow("Video");
      break;
    }
  }
  video_cap.release();
  cv::waitKey(0);
  cv::destroyAllWindows();
  return 0;
}
